/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
* @attention
*
*
© Copyright (c) 2019 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the License; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "usart.h"
#include "i2c.h"
#include "tim.h"
#include "stm32f4xx_hal.h"
#include "gpio.h"
#include "stdio.h"
/* Private variables ---------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
#define VECT_TAB_OFFSET 0x10000
int tankstatus = 0;
uint8_t zigbeecmd1[5]={0xFE,0x00,0x01,0x00,0x01};
uint8_t zigbeecmd2[5]={0xFE,0x00,0x02,0x00,0x02};
uint8_t zigbeestatu[20];
uint8_t zigbeestatu1[5];
uint8_t zigbeecon[7]="connect";
uint8_t zigbeenotcon[11]="not connect";
uint8_t zigbeechang[14]="change is down";
uint8_t cmd[7]={0xFF,0x00,0x00,0x00,0x00,0x00,0x0D};
uint16_t i,val,value_H,value_L,Cvalue,Hvalue;
int error;
float C1=-2.0468;
float C2=0.0367;
float C3=-0.0000015955;
float RH_Lin;
float RH_Ture;
float d1=-39.6;
float d2=0.01;
float T1=0.01;
float T2=0.00008;
float temp_C=0;
int temp;
int RH;
float M14_SHT11 =0;
/* USER CODE BEGIN PV */
typedef void (*pFunction)(void);
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void MX_ADC1_Init1(char pin);
void sensor(void);
void SHT10_TransStart(void);
void SHT10_WriteByte(void);
void SHT10_WriteByte2(void);
void SHT10_ReadByte(void);
void SHT10_Calculate(void);
void MX_GPIO_Input1(unsigned long pin);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int _write(int fd, char *ch, int len)
{
HAL_UART_Transmit(&huart4, (uint8_t*)ch, len, 0xFFFF);
HAL_UART_Transmit(&huart2, (uint8_t*)ch, len, 0xFFFF);
return len;
}
void print(char* s, float c)
{ int w;
int w_d;
int w_f;
if(c<0)
{
w = c*100;
w_d = w/100;
w_f = (-1*w)%100;
}
else
{
w = c*100;
w_d = w/100;
w_f = w%100;
}
printf("%s = %d.%d\n",s,w_d,w_f);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_UART4_Init();
MX_USART1_UART_Init();
HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd2, sizeof(zigbeecmd2), 10);
HAL_Delay(1000);
HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd1, sizeof(zigbeecmd1), 10);
HAL_UART_Receive(&huart2, (uint8_t *)zigbeestatu,sizeof(zigbeestatu),2000);
HAL_UART_Transmit(&huart4, (uint8_t *)zigbeestatu, sizeof(zigbeestatu),10);
if (zigbeestatu[18]== 0x01 || zigbeestatu[18]== 0x02)
{
HAL_UART_Transmit(&huart4, (uint8_t *)zigbeecon, sizeof(zigbeecon),10);
}
else if(zigbeestatu[18]== 0x05)
{
HAL_UART_Transmit(&huart4, (uint8_t *)zigbeenotcon, sizeof(zigbeenotcon),10);
}
while (1)
{
sensor();
if(M14_SHT11<16)
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_10, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_13, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET);
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 72;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 3;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
void user_pwm_setvalue(uint16_t value)
{
TIM_OC_InitTypeDef sConfigOC;
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = value;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
}
void user_pwm_setvalue2(uint16_t value)
{
TIM_OC_InitTypeDef sConfigOC;
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = value;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
}
void sensor(void)
{
Cvalue=0;
Hvalue=0;
value_H=0;
value_L=0;
SHT10_TransStart();
SHT10_WriteByte();
MX_GPIO_Input1(GPIO_PIN_0);
HAL_Delay(250);
if (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0)
{
SHT10_ReadByte();
value_H=val;
SHT10_ReadByte();
value_L=val;
Cvalue = (value_H<< 8 | value_L);
}
SHT10_TransStart();
SHT10_WriteByte2();
MX_GPIO_Input1(GPIO_PIN_0);
HAL_Delay(250);
if (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0)==0)
{
SHT10_ReadByte();
value_H=val;
SHT10_ReadByte();
value_L=val;
Hvalue = (value_H<< 8 | value_L);
}
SHT10_Calculate();
HAL_Delay(10000);
}
void SHT10_TransStart(void)
{
MX_GPIO_Init1();
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
HAL_Delay(10);
}
void SHT10_WriteByte(void)
{
MX_GPIO_Init1();
for (i=0x80;i>0;i/=2)
{
if (i & 0x03)
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
else
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
HAL_Delay(10);
}
MX_GPIO_Input1(GPIO_PIN_0);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
HAL_Delay(10);
error=HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
HAL_Delay(10);
}
void SHT10_WriteByte2(void)
{
MX_GPIO_Init1();
for (i=0x80;i>0;i/=2)
{
if (i & 0x05)
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
else
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
HAL_Delay(10);
}
MX_GPIO_Input1(GPIO_PIN_0);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
HAL_Delay(10);
error=HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
HAL_Delay(10);
}
void SHT10_ReadByte(void)
{
val=0;
MX_GPIO_Input1(GPIO_PIN_0);
for (i=0x80;i>0;i/=2)
{
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
HAL_Delay(10);
if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0))
val=( val | i );
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
}
MX_GPIO_Init1();
if (1)
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
HAL_Delay(10);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
HAL_Delay(10);
}
void SHT10_Calculate(void)
{
temp_C=d1+d2*Cvalue;
RH_Lin = C1+C2*Hvalue+C3*Hvalue*Hvalue;
RH_Ture= (temp_C-25)*(T1+T2*Hvalue)+RH_Lin;
if (RH_Ture>100)
RH_Ture= 100;
if (RH_Ture<0.1)
RH_Ture=0.1;
if (temp_C<0)
{
cmd[1]=1;
cmd[2]=-1*temp_C;
temp=(-10*temp_C);
cmd[3]=temp%10;
cmd[4]=RH_Ture;
RH=RH_Ture*10;
cmd[5]=RH%10;
}
else
{
cmd[1]=0;
cmd[2]=temp_C;
temp=(10*temp_C);
cmd[3]=temp%10;
cmd[4]=RH_Ture;
RH=RH_Ture*10;
cmd[5]=RH%10;
}
}
void MX_ADC1_Init1(char pin)
{
ADC_ChannelConfTypeDef sConfig = {0};
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = pin;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
void MX_GPIO_Input1(unsigned long pin)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOE_CLK_ENABLE();
GPIO_InitStruct.Pin = pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void Error_Handler(void)
{
}